package com.ang;

public class Rice {

    public final String lockA = "lockA";
    public final String lockB = "lockB";

    public void test(){
        new Thread(() -> {
            //获得锁A
            synchronized (lockA) {
                System.out.println(Thread.currentThread().getName() + lockA);
                //休眠100毫秒
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //获得锁B
                synchronized (lockB) {
                    System.out.println(Thread.currentThread().getName() + lockB);
                }
            }

        }).start();

        new Thread(() -> {
            //获得锁B
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + lockB);
                //休眠100毫秒
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //获得锁B
                synchronized (lockA) {
                    System.out.println(Thread.currentThread().getName() + lockA);
                }
            }
        }).start();
    }

    public static void main(String[] args) throws InterruptedException {
        Rice rice = new Rice();
        rice.test();
    }
}
